package com.example.amtf_tmdrecycle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Collections;

public class additemActivity extends AppCompatActivity {

    private EditText pname;

    private EditText ptime;

    private EditText ptime_end;

    private Button addbtn;
    private ImageView image_add;

    private MyDatabaseHelper dbHelper;
    private SQLiteDatabase db;

    private byte[] imageData;

    private Calendar calendar; // 通过Calendar获取系统时间
    private int mYear;
    private int mMonth;
    private int mDay;


    public additemActivity() {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                Bitmap bitmap = (Bitmap) data.getExtras().get("data");
                image_add.setImageBitmap(bitmap);
                int size = bitmap.getWidth() * bitmap.getHeight() * 4;

                ByteArrayOutputStream baos= new ByteArrayOutputStream(size);
                try {
                    bitmap.compress(Bitmap.CompressFormat.PNG, 30, baos);
                    imageData = baos.toByteArray();
                }catch (Exception e){
                }finally {
                    try {
                        baos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    @SuppressLint("WrongThread")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_additem);

        pname=findViewById(R.id.project_Edit);

        ptime=findViewById(R.id.time_Edit);

        ptime_end=findViewById(R.id.end_time_Edit);

        addbtn=findViewById(R.id.add_button);

        image_add=findViewById(R.id.add_image);

        dbHelper=new MyDatabaseHelper(this,"TodoList.db",null,1);
        db=dbHelper.getWritableDatabase();

        // 点击"日期"提示框 设置日期
        calendar = Calendar.getInstance();

        ptime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(additemActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year,
                                                  int month, int day) {
                                // TODO Auto-generated method stub
                                mYear = year;
                                mMonth = month;
                                mDay = day;
                                // 更新EditText控件日期 小于10加0
                                ptime.setText(new StringBuilder()
                                        .append(mYear)
                                        .append("-")
                                        .append((mMonth + 1) < 10 ? "0"
                                                + (mMonth + 1) : (mMonth + 1))
                                        .append("-")
                                        .append((mDay < 10) ? "0" + mDay : mDay));
                            }
                        }, calendar.get(Calendar.YEAR), calendar
                        .get(Calendar.MONTH), calendar
                        .get(Calendar.DAY_OF_MONTH)).show();
            }
        });

        ptime_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(additemActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year,
                                                  int month, int day) {
                                // TODO Auto-generated method stub
                                mYear = year;
                                mMonth = month;
                                mDay = day;
                                // 更新EditText控件日期 小于10加0
                                ptime_end.setText(new StringBuilder()
                                        .append(mYear)
                                        .append("-")
                                        .append((mMonth + 1) < 10 ? "0"
                                                + (mMonth + 1) : (mMonth + 1))
                                        .append("-")
                                        .append((mDay < 10) ? "0" + mDay : mDay));
                            }
                        }, calendar.get(Calendar.YEAR), calendar
                        .get(Calendar.MONTH), calendar
                        .get(Calendar.DAY_OF_MONTH)).show();
            }
        });

        image_add.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent1,1);
            }
        });



        addbtn.setOnClickListener(new View.OnClickListener() {              //按下添加按钮   将名称和时间传递给主界面
            @Override
            public void onClick(View v) {
                String name=pname.getText().toString();
                String time=ptime.getText().toString();
                String end_time=ptime_end.getText().toString();
                Todolistinfo info;
                if(!name.equals("")&&!time.equals("")&&!end_time.equals("")&& imageData !=null){
                    info = new Todolistinfo(name, time,end_time,imageData);
                }else {
                    Toast toast=Toast.makeText(getApplicationContext(), R.string.tishi, Toast.LENGTH_SHORT);
                    toast.show();
                    return;
                }
                //Todolistinfo info = new Todolistinfo(name, time,end_time);
                ContentValues values=new ContentValues();
                values.put("time_start",info.getTime());
                values.put("time_end",info.getEnd_time());
                values.put("project",info.getName());
                values.put("progress",info.getProgress());
                values.put("image",info.getImage());
                db.insert("TodoList",null,values);
                Intent intent=new Intent();
                intent.putExtra("todolist1",info);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }
}
